package xhwl.insurance.customView;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import xhwl.android.common.util.StringUtil;
import xhwl.android.common.util.ToastUtil;
import xhwl.insurance.R;
import xhwl.insurance.util.XHLog;


/**
 * Created by fuyi on 2017/9/27.
 */

public class PlayerWebView extends FrameLayout {

    private static final String TAG = PlayerWebView.class.getSimpleName();
    private Context mContext;
    private RelativeLayout rlCover;
    private ImageView ivCover;
    private ImageView ivPlay;
    private WebView webView;
    boolean isPalying = false;
    private String videoUrl;
    Handler handler = new Handler();

    public PlayerWebView(Context context) {
        this(context, null);
    }

    public PlayerWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        LayoutInflater.from(context).inflate(R.layout.player_webview, this);
        rlCover = (RelativeLayout) findViewById(R.id.rlCover);
        ivCover = (ImageView) findViewById(R.id.ivCover);
        ivPlay = (ImageView) findViewById(R.id.ivPlay);
        webView = (WebView) findViewById(R.id.webView);

        initWebView();
        ivPlay.setOnClickListener(listener);
    }

    private void initWebView() {

        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);//不加上白边
        webView.setBackgroundColor(Color.parseColor("#000000"));
//        webView.setLayerType();android:layerType
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setPluginState(WebSettings.PluginState.ON);
        webSettings.setUseWideViewPort(true);
        webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH);

        // 启用localStorage 和 essionStorage
        webSettings.setDomStorageEnabled(true);

        // 开启应用程序缓存z
        webSettings.setAppCacheEnabled(true);
        String appCacheDir = getContext().getDir("cache", Context.MODE_PRIVATE).getPath();
        webSettings.setAppCachePath(appCacheDir);
        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
        webSettings.setAppCacheMaxSize(1024 * 1024 * 100);// 设置缓冲大小，我设的是10M
        webSettings.setAllowFileAccess(true);
        webSettings.setUserAgentString(
                "Mozilla/5.0 (iPhone; CPU iPhone OS 7_1 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Version/7.0 Mobile/11D5145e Safari/9537.53");
        webView.setWebChromeClient(new WebChromeClient());
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                webView.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                XHLog.d("onPageFinished");

                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        webView.loadUrl("javascript:playVideo()");
                    }
                }, 5000);
            }

        });
    }

    private OnClickListener listener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.ivPlay:
                    if (callBack != null) {
                        callBack.onPlayBtnClick();
                    }
                    break;
                default:
                    break;
            }
        }
    };
    CallBack callBack;

    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }

    public interface CallBack {
        void onPlayBtnClick();

    }

    public void setVideoUrl(String url) {
        this.videoUrl = url;

    }

    public void setCoverImageUrl(String url) {
        Glide.with(mContext)
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .skipMemoryCache(true)
                .error(R.drawable.pic_add)
                .into(ivCover);
    }


    public void Suspend() {
        webView.loadUrl("javascript:pauseVideo()");
    }

    public void play() {
        if (StringUtil.isEmpty(videoUrl)) {
            ToastUtil.showToast(mContext, "url为空");
            return;
        }

        if (!isPalying) {
            isPalying = true;
            rlCover.setVisibility(View.GONE);
            webView.loadUrl(videoUrl);

        }
    }

    public void pause() {
        if (isPalying) {
            isPalying = false;
            rlCover.setVisibility(View.VISIBLE);
            webView.loadUrl("javascript:pauseVideo()");
        }
    }

}
